function ConnectionClass(debug)
{
    if(debug != undefined)
    {
        var debug = debug;
    }
    else
    {
        var debug = false;
    }
    var incoming = 0;
    var outgoing = 0;
    var ready = false;
    var messageCallback = function(){};
    this.start = function(callback)
    {
        messageCallback = callback;
        init();
    }
    function createSocket(host)
    {
        if (window.WebSocket)
            return new WebSocket(host);
        else if (window.MozWebSocket)
            return new MozWebSocket(host);
    }

    this.ready = function()
    {
        return ready;
    }

    function init()
    {
        // alert(document.location.hostname)
        var hostname = document.location.hostname;
        var host = "ws://" + hostname + ":8888/websocket";
        try
        {
            socket = createSocket(host);
            log('WebSocket - status ' + socket.readyState);
            socket.onopen = function(msg)
            {
               log("Welcome - status " + this.readyState);
               ready = true;
            };
            socket.onmessage = function(msg)
            {
                incoming += msg.data.length;
                log("Received (" + incoming + "b): " + msg.data);
                messageCallback(msg.data);
            };
                socket.onclose = function(msg)
            {
                ready = false;
            };
        }
        catch (ex)
        {
           log(ex);
        }
    }

    this.send = function(msg)
    {
        try
        {
            outgoing += msg.length;
            socket.send(msg);
            log("Sent ("+outgoing+"b): " + msg);
        }
        catch (ex)
        {
            log(ex);
        }
    }
    function quit()
    {
        log("Goodbye!");
        socket.close();
        socket = null;
    }

    function log(msg)
    {
        if(debug)
            console.log(msg);
    }
}